package base.day12;

import java.util.ArrayList;

public class Arraylist18 {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Student stu1=new Student("张三",10);
        Student stu2=new Student("李四",11);
        Student stu3=new Student("王五",19);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        ArrayList<Student> result = getArrayList(list);
        for (int i = 0; i < result.size(); i++) {
            Student stu=result.get(i);
            System.out.println(stu.getName()+","+stu.getAge());
        }


    }
    public static ArrayList<Student> getArrayList(ArrayList<Student> list){
        //定义新集合
        ArrayList<Student> newlist=new ArrayList();
        //遍历list中的元素
        for (int i = 0; i < list.size(); i++) {
            //得到list中的元素
            Student stu=list.get(i);
            //判断年龄是否大于18，存入newlist
            if (stu.getAge()>18){
                newlist.add(stu);
            }
        }
        return newlist;
    }
}
